package com.example.teaculture.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.teaculture.DB.FavorDao;
import com.example.teaculture.DB.HistoryDao;
import com.example.teaculture.R;
import com.example.teaculture.activity.FavorDetailActivity;
import com.example.teaculture.activity.MainActivity;
import com.example.teaculture.adapter.HeadlineBaseAdapter;
import com.example.teaculture.adapter.KnowledgeBaseAdapter;
import com.example.teaculture.application.MyApplication;
import com.example.teaculture.model.Favor;
import com.example.teaculture.model.Headline;
import com.example.teaculture.model.Teaknown;
import com.example.teaculture.view.RefreshableView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

public class KnowledgeFragment extends androidx.fragment.app.Fragment {

    RefreshableView refreshableView;

    Handler handler;

    List<Teaknown> teaKnowns;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.knowledge_fragment, container, false);
        ListView lv_view = view.findViewById(R.id.list_view);


        teaKnowns = MyApplication.getInstance().teaKnowns;
        KnowledgeBaseAdapter knowledgeBaseAdapter = new KnowledgeBaseAdapter(getContext(), teaKnowns);
        lv_view.setAdapter(knowledgeBaseAdapter);

        handler = new Handler(message -> {
            if (message.what == 0) {

                KnowledgeBaseAdapter baseAdapter1 = new KnowledgeBaseAdapter(getContext(), teaKnowns);
                lv_view.setAdapter(baseAdapter1);

            }
            return false;
        });

        lv_view.setOnItemClickListener((adapterView, view1, i, l) -> {
            Favor favor = new Favor();
            favor.setTitle(teaKnowns.get(i).getTitle());
            favor.setUrl(teaKnowns.get(i).getUrl());
            MyApplication.getInstance().favor = favor;
            Intent intent = new Intent(getContext(), FavorDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.putExtra("url", teaKnowns.get(i).getUrl());
            intent.putExtra("title", teaKnowns.get(i).getTitle());
            startActivity(intent);
        });

        refreshableView = (RefreshableView) view.findViewById(R.id.refreshable_view);
        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                try {

                    teaKnowns.clear();
                    Document doc = Jsoup.connect("https://chaaowang.com/fenlei/changshi/page/2").get();
                    Elements elements = doc.select("ul.post-loop.post-loop-default.cols-0");
                    elements = elements.select("li.item");
                    for (Element element : elements) {
                        String title = element.select("div.item-content").select("h2.item-title").select("a").text();
                        String date = element.select("div.item-content").select("div.item-meta").select("span.item-meta-li.date").text();
                        String newDate = date.replace("年", "-").replace("月", "-").replace("日", "");
                    }
                    Message message = Message.obtain();
                    message.what = 0;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                refreshableView.finishRefreshing();
            }
        }, 1);


        return view;
    }
}
